package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.gson.Gson;
import com.wanglibao.woodtrade.woodtrade.ItemBuyActivityAdapter;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.DemandDetail;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.user.LoginActivity;
import com.wanglibao.woodtrade.woodtrade.util.ToastUtil;
import com.wanglibao.woodtrade.woodtrade.widget.SVGridView;

import butterknife.Bind;
import butterknife.ButterKnife;
/*
  出售商品的详情页
 */

public class DemandActivity extends BaseActivity {

    @Bind(R.id.rl_contact_buy_activity)
    RelativeLayout rlContactBuyActivity;
    @Bind(R.id.tv_contact_title_buy_avtivity)
    TextView tvContactTitleBuyAvtivity;
    @Bind(R.id.fengexian)
    TextView fengexian;
    @Bind(R.id.ll_contact_buy_activity)
    RelativeLayout llContactBuyActivity;
    @Bind(R.id.tv_contact_price_buy_avtivity)
    TextView tvContactPriceBuyAvtivity;
    @Bind(R.id.ll_shanpinxiangqing_demand_activity)
    LinearLayout llShanpinxiangqingDemandActivity;
    @Bind(R.id.tv_introduction_demand_activity)
    TextView tvIntroductionDemandActivity;
    @Bind(R.id.ll_information_demand_activity)
    LinearLayout llInformationDemandActivity;
    @Bind(R.id.tv_pinming_demand_activity)
    TextView tvPinmingDemandActivity;
    @Bind(R.id.tv_guige_demand_activity)
    TextView tvGuigeDemandActivity;
    @Bind(R.id.tv_kucun_demand_activity)
    TextView tvKucunDemandActivity;
    @Bind(R.id.tv_cunchudi_demand_activity)
    TextView tvCunchudiDemandActivity;
    @Bind(R.id.tv_qidingliang_demand_activity)
    TextView tvQidingliangDemandActivity;
    @Bind(R.id.bt_contact_buy_avtivity)
    Button btContactBuyAvtivity;
    @Bind(R.id.tv_title_shangpin_bianhao)
    TextView mTvTitleShangpinBianhao;
    @Bind(R.id.tv_shangpin_bianhao)
    LinearLayout mTvShangpinBianhao;
    @Bind(R.id.tv_chandi_demand_activity)
    TextView mTvChandiDemandActivity;
    @Bind(R.id.tv_hongxian)
    TextView mTvHongxian;
    @Bind(R.id.rv_editDemand_activity)
    SVGridView mRvEditBuyActivity;
    @Bind(R.id.tv_hot_contact_demand_activity)
    TextView mTvHotContactDemandActivity;

    //==================================
    DemandDetail mDetails = null;
    ItemBuyActivityAdapter mAdapter;
    Gson mGson = new Gson();
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.arg2 != 2) {
                mDetails = mGson.fromJson(msg.obj.toString(), DemandDetail.class);
                appearData();
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demand);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            rl_common_title.setPadding(0, 0, 0, 0);
        }
        NetWorking();
        initData();
    }

    private void initData() {
        mBasIn = new BounceTopEnter();
        mBasOut = new SlideBottomExit();
        tvTitle.setText("商品详情");
        addViewClicklistener(tvBack, btContactBuyAvtivity);
    }

    public void NetWorking() {
        String id = getIntent().getStringExtra("ID");
        JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask(this, NetConfig.SERVER + NetConfig.TYPE_MESSAGE,
                NetConfig.messageSendDetail, mHandler, "jsonObject", NetConfig.TaskId.messageSendDetail, new Object[]{id, 1});
        jsonRPCAsyncTask.execute(new Void[]{});
    }

    public void appearData() {
        mTvTitleShangpinBianhao.setText("商品编号：" + mDetails.getNumbers());
        tvContactTitleBuyAvtivity.setText(mDetails.getTitle());
        if(mDetails.getImg_ablum_list().size()>0){
            mAdapter=new ItemBuyActivityAdapter(this,mDetails.getImg_ablum_list());
            mRvEditBuyActivity.setAdapter(mAdapter);
        }
        if(mDetails.getWant_price().equals("面议")){
            tvContactPriceBuyAvtivity.setText("面议");
        }else{
            tvContactPriceBuyAvtivity.setText("￥" + mDetails.getWant_price() + "元/" + mDetails.getUnit());
        }
        mTvHotContactDemandActivity.setText("热度："+mDetails.getMessage_click());
        tvPinmingDemandActivity.setText(mDetails.getCategory_name());
        tvGuigeDemandActivity.setText(mDetails.getSize());
        mTvChandiDemandActivity.setText(mDetails.getPlace());
        tvKucunDemandActivity.setText(mDetails.getNumber());
        tvCunchudiDemandActivity.setText(mDetails.getDepot_province_name() + "|" + mDetails.getDepot_city_name());
        tvQidingliangDemandActivity.setText(mDetails.getOrder_number());
    }

    private BaseAnimatorSet mBasIn;
    private BaseAnimatorSet mBasOut;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.imgbtn_commonTitle_back:
                finish();
                overridePendingTransition(R.anim.extre_in, R.anim.extre_out);
                break;
            case R.id.bt_contact_buy_avtivity:
                if (new UserImpl(this).isLogin()) {
                    showDialog();
                } else {
                    startActivity(new Intent(this, LoginActivity.class));
                }
                break;
            default:
                break;
        }
    }

    Dialog dialog;

    private void showDialog() {
        dialog = new Dialog(this, R.style.dialog_bgTransparent);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        dialog.getWindow().setContentView(R.layout.dialog_tel);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_dialog_title);
        tv_title.setText("请牢记单号并告知客服");
        TextView tv_contact = (TextView) dialog.findViewById(R.id.tv_dialog_contact);
        tv_contact.setText(mDetails.getNumbers());
        TextView tv_cancle = (TextView) dialog.findViewById(R.id.tv_dialog_tel_cancle);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView tv_tel = (TextView) dialog.findViewById(R.id.tv_dialog_call);
        tv_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDetails != null) {
//                    requestPermission();
                    final String forPhoneNum = mDetails.getPhone();
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.CALL");
                    intent.setData(Uri.parse("tel:" + forPhoneNum));
                    startActivity(intent);
                }
            }
        });
    }

    /*操作权限的动态请求*/
    public void requestPermission() {
        //判断当前Activity是否已经获得了该权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            //如果App的权限申请曾经被用户拒绝过，就需要在这里跟用户做出解释
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
                ToastUtil.createNormalToast("please give me the permission");
            } else {
                //进行权限请求
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 10);
            }
        } else {

        }
    }

    private void NormalDialogStyleTwo() {
        final NormalDialog dialog = new NormalDialog(this);
        dialog.content(mDetails.getNumber())//
                .contentTextColor(R.color.orange)
                .style(NormalDialog.STYLE_TWO)//
                .titleTextSize(18)//
                .title("请牢记单号并告知客服")
                .showAnim(mBasIn)//
                .dismissAnim(mBasOut)//
                .show();
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                        dialog.btnText("拨号");
                        final String forPhoneNum = mDetails.getPhone();
                        Intent intent = new Intent();
                        intent.setAction("android.intent.action.CALL");
                        intent.setData(Uri.parse("tel:" + forPhoneNum));
                        startActivity(intent);
                    }
                });
    }
}
